package com.atguigu.cloud.controller;

import com.atguigu.cloud.entities.Pay;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.response.ResponseCodeEnum;
import com.atguigu.cloud.response.ResultData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@Tag(name = "订单微服务模块", description = "订单交易CRUD")
@RequestMapping("/order")
@RestController
@Slf4j
public class OrderController {

    /**
     * 支付微服务地址
     */
    private static final String PAY_URL = "http://localhost:8001";
    private static final String PAY_ADD_URL = "/pay/add";
    private static final String PAY_DELETE_URL = "/pay/delete/{id}";
    private static final String PAY_UPDATE_URL = "/pay/update";
    private static final String PAY_GETBYID_URL = "/pay/getById/{id}";
    private static final String PAY_GETALL_URL = "/pay/getAll";

    @Resource
    private RestTemplate restTemplate;

    /**
     * 订单新增支付信息
     *
     * @param payDTO 支付信息数据传输对象
     * @return ResultData
     */
    @PostMapping(PAY_ADD_URL)
    @Operation(summary = "订单新增支付信息", description = "订单新增支付操作")
    @Parameter(name = "payDTO", description = "支付信息数据传输对象")
    public ResultData addOrderPay(@RequestBody PayDTO payDTO) {
        log.info("订单新增支付信息：{}", payDTO);
        return restTemplate.postForObject(PAY_URL + PAY_ADD_URL, payDTO, ResultData.class);
    }

    /**
     * 订单删除支付信息
     *
     * @param id 支付id
     * @return ResultData
     */
    @DeleteMapping(PAY_DELETE_URL)
    @Operation(summary = "订单删除支付信息", description = "订单删除支付操作")
    @Parameter(name = "id", description = "支付id", required = true, example = "1")
    public ResultData deleteOrderPay(@PathVariable("id") Integer id) {
        log.info("订单删除支付信息：{}", id);
        try {
            restTemplate.delete(PAY_URL + PAY_DELETE_URL, id);
            return ResultData.success(1, ResponseCodeEnum.RC204);
        } catch (Exception e) {
            return ResultData.fail(e.getMessage());
        }
    }

    /**
     * 订单更新支付信息
     *
     * @param pay 支付信息
     * @return ResultData
     */
    @PutMapping(PAY_UPDATE_URL)
    @Operation(summary = "订单更新支付信息", description = "订单更新支付操作")
    @Parameter(name = "pay", description = "支付信息")
    public ResultData updateOrderPay(@RequestBody Pay pay) {
        log.info("订单更新支付信息：{}", pay);
        try {
            restTemplate.put(PAY_URL + PAY_UPDATE_URL, pay);
            return ResultData.success(1, "更新成功");
        } catch (Exception e) {
            return ResultData.fail(e.getMessage());
        }
    }

    /**
     * 订单根据id查询支付信息
     *
     * @param id 支付id
     * @return ResultData
     */
    @GetMapping(PAY_GETBYID_URL)
    @Operation(summary = "订单根据id查询支付信息", description = "订单根据id查询支付操作")
    @Parameter(name = "id", description = "支付id", required = true, example = "1")
    public ResultData getOrderPayById(@PathVariable("id") Integer id) {
        log.info("订单根据id查询支付信息：{}", id);
        return restTemplate.getForObject(PAY_URL + PAY_GETBYID_URL, ResultData.class, id);
    }

    /**
     * 订单查询所有支付信息
     * @return ResultData
     */
    @GetMapping(PAY_GETALL_URL)
    @Operation(summary = "订单查询所有支付信息", description = "订单查询所有支付操作")
    public ResultData getOrderPayAll() {
        log.info("订单查询所有支付信息");
        return restTemplate.getForObject(PAY_URL + PAY_GETALL_URL, ResultData.class);
    }
}
